<script setup lang='ts' name="video-management-add">
import type { FormInstance } from 'element-plus'

import { videoUpdate } from '@fl/api/fangjiaohui'
import UpdatedOss from '@fl/components/upLoad-oss.vue'
import { ElButton, ElDialog, ElForm, ElFormItem, ElInput, ElMessage } from 'element-plus'
import { cloneDeep } from 'lodash-es'
import { nextTick, toRefs } from 'vue'

const props = withDefaults(defineProps<{
    data?: any
    modelValue: boolean
    videoType: any
}>(), {
    data: () => {},
})

const emits = defineEmits(['update:modelValue', 'confirm'])
const { data, modelValue, videoType } = toRefs(props)

const state = { catId: '', img: '', info: '', title: '', url: '' }
const form = ref<any>({ ...state })

const title = ref('新增')
const formRef = ref()

const rules = reactive<any>({
    catId: [
        { message: '请选择', required: true, trigger: 'blur' },
    ],
    img: [
        { message: '请上传', required: true, trigger: 'blur' },
    ],
    info: [
        { message: '请输入', required: true, trigger: 'blur' },
    ],
    title: [
        { message: '请输入', required: true, trigger: 'blur' },
    ],
    url: [
        { message: '请上传', required: true, trigger: 'blur' },
    ],
})

async function doSave(formEl: FormInstance | undefined) {
    if (!formEl)
        return
    await formEl.validate((valid, fields) => {
        if (valid) {
            const catName = videoType.value.find((v: any) => v.id === form.value.catId)?.name
            videoUpdate({ ...form.value, catName }).then((res: any) => {
                if (res.code === 200) {
                    close(formEl)
                    emits('confirm')
                }
                else {
                    ElMessage({ message: res.msg, type: 'error' })
                }
            }).catch((err) => {
                const message = err?.response?.data?.message || err
                ElMessage({ message, type: 'error' })
            })
        }
        else { console.log('error submit!', fields) }
    })
}

function close(formEl: FormInstance | undefined) {
    formEl && formEl.resetFields()
    emits('update:modelValue', false)
}

async function init(val: any) {
    await nextTick()
    if (val?.id) {
        title.value = '编辑'
        form.value = cloneDeep(val)
    }
    else {
        title.value = '新增'
        form.value = { ...state }
    }
}

watch(data, (val: any) => {
    if (val)
        init(val)
}, {
    deep: true,
    immediate: true,
})
</script>

<template>
    <ElDialog v-model="modelValue"
              width="600"
              :close-on-click-modal="false"
              :title="title"
              @close="close"
    >
        <ElForm ref="formRef"
                :model="form"
                :rules="rules"
                label-width="auto"
                label-position="top"
                style="max-width: 500px"
        >
            <ElFormItem label="视频分类"
                        prop="catId"
            >
                <ElSelect v-model="form.catId">
                    <ElOption v-for="item in videoType"
                              :key="item.id"
                              :value="item.id"
                              :label="item.name"
                    />
                </ElSelect>
            </ElFormItem>

            <ElFormItem label="视频标题"
                        prop="title"
            >
                <ElInput v-model="form.title" />
            </ElFormItem>

            <ElFormItem label="视频说明"
                        prop="info"
            >
                <ElInput v-model="form.info" />
            </ElFormItem>

            <ElRow>
                <ElCol :span="12">
                    <ElFormItem label="视频上传"
                                prop="url"
                    >
                        <UpdatedOss v-model="form.url"
                                    :is-video="true"
                                    :size="1024"
                                    accept=".mp4,.avi"
                        />
                    </ElFormItem>
                </ElCol>
            </ElRow>

            <ElRow>
                <ElCol :span="12">
                    <ElFormItem label="视频封面"
                                prop="img"
                    >
                        <UpdatedOss v-model="form.img"
                                    accept=".png,.jpg,.jpeg"
                        />
                    </ElFormItem>
                </ElCol>
            </ElRow>

            <div style="text-align: center;">
                <ElButton type="primary"
                          @click="doSave(formRef)"
                >
                    确定
                </ElButton>

                <ElButton @click="close(formRef)">
                    取消
                </ElButton>
            </div>
        </ElForm>
    </ElDialog>
</template>
